<?php

namespace app\modules\admin\controllers;

use app\modules\admin\models\LoginForm;
use app\modules\admin\models\SignupForm;
use Yii;
use yii\web\Controller;

/**
 * Default controller for the `admin` module
 */
class DefaultController extends Controller
{
    /**
     * Renders the index view for the module
     * @return string
     */
    public function actionIndex()
    {
        return $this->render('index');
    }

    public function actionLogin(){
        if (!\Yii::$app->user->isGuest) {
            return $this->redirect(['/admin/default/index']);
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->redirect(['/admin/default/index']);
        }
        return $this->renderPartial('login', [
            'model' => $model,
        ]);

    }

    public function actionLogout()
    {
        if (Yii::$app->user->logout()){
            return $this->redirect(['/admin/default/login']);
        }
    }

    public function actionSignup(){
        $model = new SignupForm();
        if($model->load(Yii::$app->request->post())) {
            if($model->signup()) {
                return $this->redirect(['/admin/default/login']);
            }
        }
        return $this->renderPartial('signup', [
            'model' => $model,
        ]);
    }

    public function actionChangePass(){
        $model = new ChangePassForm();
        if ($model->load(Yii::$app->request->post()) && $model->update() ) {
            Yii::$app->user->logout();
            return $this->redirect(['/admin/default/login']);
        }
        return $this->render('change-pass',[
            'model' => $model
        ]);
    }
}
